package com.seesmic
{
	import com.seesmic.helper.VersionManager;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.media.Video;
	import com.seesmic.events.SeesmicResultEvent;
	
	 
	public class Recorder extends Sprite
	{
		
		private var versionManager:VersionManager		
		private var seesmicService:SeesmicService
		
		public function Recorder(seesmicService:SeesmicService)
		{
			//TODO: implement function
			super();
			
			this.seesmicService = seesmicService
			 
			versionManager = VersionManager.getInstance();
			if(versionManager.ready)
			{
				var swfName:String = versionManager.getSWF(VersionManager.RECORDER)
				loadSWF(swfName)
			}
			else
			{
				versionManager.addEventListener("ready", onReady)
			}
			
			
		}
		
		private function onReady(e:Event):void
		{
			versionManager.removeEventListener("ready", onReady)
			var swfName:String = versionManager.getSWF(VersionManager.RECORDER)
			loadSWF(swfName)
				
		}
		
		private function loadSWF(swfName:String):void
		{
			swfloader= new Loader();
			swfloader.addEventListener(Event.INIT, onLoadInit)
			swfloader.addEventListener(IOErrorEvent.IO_ERROR, onSWFError);
			swfloader.visible = false
			swfloader.load(new URLRequest(swfName), new LoaderContext(false, ApplicationDomain.currentDomain))
			this.addChild(swfloader)
		}
		
		private var displayObject:DisplayObject
		private var swfloader:Loader
		private function removeSWFLoaderEventListener()
		{
			swfloader.removeEventListener(Event.INIT, onLoadInit)
			swfloader.removeEventListener(IOErrorEvent.IO_ERROR, onSWFError);
		}
		
		private function onSWFError():void
		{
			removeSWFLoaderEventListener()
			serverError()
		}

		private function onLoadInit(e:Event):void
		{
			removeSWFLoaderEventListener()
			displayObject =  e.target as  DisplayObject
			swfloader.visible = true
			
			displayObject.width = __width
			displayObject.height  = __height
			//trace("RECORDER READY")
			if(seesmicService.sid != null)
			{
				displayObject["setCredentials"](seesmicService.sid, seesmicService.username)	
			}
			
			displayObject.addEventListener("VIDEO_POSTED", onVideoPosted)
			
			//	var event:ObjectDataEvent = new ObjectDataEvent("VIDEO_POSTED", video)		
			//Application.application.dispatchEvent(event)
		}
		
		private function onVideoPosted(e:Object):void
		{
			var object:Object = e.data
			var videoVO:VideoVO = new VideoVO()
			videoVO.thread_posts = object.conversationVideos
			videoVO.created_at  = object.created
			videoVO.url_flv = object.flv
			videoVO.source = object.platform
			videoVO.source_server = object.platform
			videoVO.url_thumbnail = object.thumbnail
			videoVO.video_id = object.platformId
			videoVO.username = object.publisherName
			videoVO.title = object.title
			this.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.VIDEOS_POST_VIDEO, videoVO))
		}		
		
		public function setReplyVideo(video_id:String):void
		{
			displayObject["setReplyVideo"](video_id);
		}
		
		/*
			public function setCredentials(sid:String, username:String):void
		{
			trace("sending credentials " + sid + " " + username)
			model.session_id = sid
			model.currentUser = UserModelLocator.getInstance().getUser(username)
			model.appState = SeesmicState.USER_LOGINED;
		}
		public function setReplyVideo(inreplyto:String):void
		{
			model.inreplyto = "http://www.seesmic.com/video/" + inreplyto
		}*/
		private var __width:Number = 300
		private var __height:Number = 250
		
		//private var errorCount:Number = 0
		private function serverError():void
		{
		/*
			errorCount ++;
			if(errorCount > 2)
			{
				showError("someone kicked a cable, we'll be back soon")
				return;
			}
			var server:String = "http://seesmic.com/embeds/"
				switch(_version)
				{
					case "wpplayer":
					_path = server + "StandalonePlayer.swf"
					break;
					case "wprecorder":
					_path = server + "StandaloneRecorder.swf"
					break;
					case "disqusrecorder":
					_path = server + "disqus-recorder.swf"
					break;
					case "friendfeedrecorder" :
					_path = server + "friendfeedrecorder.swf"
					break;
					case "recorder":
					_path = server + "recorder.swf"
					break;
					case "player":
					_path = server + "player.swf"
					break;
					default:
					showError("Error. The version is not a valid entry")
					break;
					
				}
				initLoader()*/
		}
		
		
	}
}